/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package negocio;

import controladores.ConvenioFacadeLocal;
import entidades.Convenio;
import entidades.Entidad;
import entidades.Estudiante;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;

/**
 *
 * @author Angel
 */
@Stateless(mappedName="ConvenioEJB")
@TransactionManagement(TransactionManagementType.BEAN)
public class ConvenioSrv implements ConvenioInterface {

    @Resource
    private EJBContext eJBContext;
    private UserTransaction userTransaction;
    private UserTransaction utx;
    @EJB
    private ConvenioFacadeLocal convenioFacadeLocal;

    @Override
    public Convenio consultarConvenio(Estudiante estudiante) {
        return null; /*convenioSessionBean.consultarPorEstudiante(estudiante);*/
    }

    @Override
    public void crear(Convenio convenio) {
        try{
            utx = eJBContext.getUserTransaction();
            utx.begin();
            convenioFacadeLocal.create(convenio);
            utx.commit();
        } catch (NotSupportedException ex) {
            Logger.getLogger(ConvenioSrv.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RollbackException ex) {
            Logger.getLogger(ConvenioSrv.class.getName()).log(Level.SEVERE, null, ex);
        } catch (HeuristicMixedException ex) {
            Logger.getLogger(ConvenioSrv.class.getName()).log(Level.SEVERE, null, ex);
        } catch (HeuristicRollbackException ex) {
            Logger.getLogger(ConvenioSrv.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(ConvenioSrv.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalStateException ex) {
            Logger.getLogger(ConvenioSrv.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SystemException ex) {
            Logger.getLogger(ConvenioSrv.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public Integer obtenerMaxId() {
        return convenioFacadeLocal.obtenerMaxId();
    }

    @Override
    public List<Convenio> consultarConvenioXEntidad(Integer idEntidad) {
        return convenioFacadeLocal.listaCoonveniosXEntidad(idEntidad);
    }

    @Override
    public List<Convenio> listarConvenios() {
        return convenioFacadeLocal.findAll();
    }

    @Override
    public void actualizarConvenio(Convenio convenio) {
        try{
            utx = eJBContext.getUserTransaction();
            utx.begin();
            convenioFacadeLocal.edit(convenio);
            utx.commit();
        } catch (NotSupportedException ex) {
            Logger.getLogger(ConvenioSrv.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RollbackException ex) {
            Logger.getLogger(ConvenioSrv.class.getName()).log(Level.SEVERE, null, ex);
        } catch (HeuristicMixedException ex) {
            Logger.getLogger(ConvenioSrv.class.getName()).log(Level.SEVERE, null, ex);
        } catch (HeuristicRollbackException ex) {
            Logger.getLogger(ConvenioSrv.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(ConvenioSrv.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalStateException ex) {
            Logger.getLogger(ConvenioSrv.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SystemException ex) {
            Logger.getLogger(ConvenioSrv.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
